<?php
class fwListGroup
{
	public $id;
	
	public $status = 'none';
	public $fields = array();
	public $fieldsIndex = array();
	public $groupsTypes = array();
	public $groups = array();
	public $order = array();
	public $onSelectChange = '';
	
	public $datasLength = 0;
	public $datasIndex = 0;

	public function __construct($id)
	{
		$this->id = $id;
		$_SESSION['__listGroup_'.$id] = &$this;
	}
	
	public function formatJavascript()
	{
		$response = array(
			'id' => $this->id,
			'datasLength' => $this->getDatasLength(),
			'onSelectChange' => $this->onSelectChange,
			'showGroupBox' => false,
			'groupBox' => false,
			'order' => $this->order,
			'fields' => array(),
		);
		$this->fieldsIndex = array();
		foreach($this->fields as $field)
		{
			$this->fieldsIndex[$field['name']] = $field;
			$response['fields'][] = array(
				'name' => $field['name'],
				'label' => $field['label'], 
				'content' => $field['content'], 
				'index' => $field['index'],
				'width' => $field['width'],
				'resizable' => $field['resizable'], 
				'data_field' => $field['data_field'], 
			);
		}
		$this->sortDatas();
		return json_encode($response);
	}
	
	public function formatJavascriptDatas($index, $length)
	{
		$response = array(
			'index' => $index,
			'length' => $length,
			'datasLength' => $this->getDatasLength(),
			'order' => $this->order,
			'datas' => $this->getDatas($index, $length)
		);
		return json_encode($response);
	}
	
	public static function get($id)
	{
		if(get_class($_SESSION['__listGroup_'.$id]) == '__PHP_Incomplete_Class')
		{
			session_write_close();
			session_start();
		}
		return $_SESSION['__listGroup_'.$id];
	}
	
	
	public function getDatasLength()
	{
		throw new Exception("not implemented");
	}
	
	public function getDatas()
	{
		throw new Exception("not implemented");
	}

	public function sortDatas()
	{
		throw new Exception("not implemented");
	}
	

	public function addOrderClose($field)
	{
		$index = false;
		foreach($this->order as $i => $order)
		{
			if($order['name'] == $field)
			{
				$index = $i;
				break;
			}
		}
		if($index === false)
		{
			array_unshift($this->order, array('name' => $field, 'sens' => 'DESC'));
		}
		else
		{
			$sens = $this->order[$index]['sens'];
			array_splice($this->order, $index, 1);
			array_unshift($this->order, array('name' => $field, 'sens' => ($sens=='ASC'?'DESC':'ASC')));
		}
	}
}
?>
